package init.luoyu.action.chapter1;



import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author LuoYu
 * date 2020/11/18
 */
public class AppleFilters {

    public static List<Apple> apples = Arrays.asList(new Apple("red",200),
            new Apple("green",100),
            new Apple("red",120));



    //筛选绿色的苹果
    public static List<Apple> filterGreenApples(List<Apple> inventory){
        ArrayList<Apple> apples = new ArrayList<>();
        for (Apple apple : inventory) {
            if("green".equals(apple.getColor())){
                apples.add(apple);
            }
        }
        return apples;
    }

    //筛选重量大于150的苹果
    public static List<Apple> filterHeavyApples(List<Apple> inventory){
        ArrayList<Apple> apples = new ArrayList<>();
        for (Apple apple : inventory) {
            if(apple.getWeight() > 150){
                apples.add(apple);
            }
        }
        return apples;
    }


    public static List<Apple> filterHeavyApples(List<Apple> inventory,AppleFilter filter){
        ArrayList<Apple> apples = new ArrayList<>();
        for (Apple apple : inventory) {
            if(filter.test(apple)){
                apples.add(apple);
            }
        }
        return apples;
    }



    public static void main(String[] args){
        List<Apple> apples = filterHeavyApples(AppleFilters.apples, new AppleFilter() {
            @Override
            public boolean test(Apple apple) {
                return apple.getColor().equals("red");
            }
        });

        apples.forEach(Apple::printf);


    }




}

interface AppleFilter{
    boolean test(Apple apple);
}

interface AppleAccet<T>{
    void accet(T t);
}



